local dao = require "dao.authdbdao"
local filelog = require "filelog"
local timetool = require "timetool"
local json = require "cjson"
local configdao = require "configdao"
local PlayerauthDAO = {}


json.encode_sparse_array(true,1,1)
--[[
	request {
		user_name,	   玩家的账号id
		rediscmd,  redis操作命令
		rediskey,  redis数据key
		--都是可选的使用时一定是按顺序添加
		rediscmdopt1, redis命令选项1
		rediscmdopt2, redis命令选项2
		rediscmdopt3,
		rediscmdopt4,
		rediscmdopt5,

		mysqltable,  mysql数据表名
		mysqldata, mysqldata表示存入mysql的数据, 一定是table
		mysqlcondition, 是一个表格或是字符串（字符串表示完整sql语句）
		choosedb, 1 表示redis， 2表示mysql，3表示redis+mysql
	}

	response {
		issuccess, 表示成功或失败
		isredisormysql, true表示返回的是mysql数据 false表示是redis数据（业务层可能要进行不同的处理）
		data, 返回的数据
	}
]]
function PlayerauthDAO.query_player_uidbyusername(user_name)
	local responsemsg
	if user_name == nil then
		filelog.sys_error("PlayerauthDAO.query_player_uidbyusername user_name == nil")		
		return false, nil
	end
	local userinfo = nil
	local requestmsg = {
		user_name = user_name,
		rediscmd = "get",
		rediskey = "user:"..user_name,
		--都是可选的使用时一定是按顺序添加

		mysqltable = "user_auth",
		mysqlcondition = {
			user_name = user_name,
		},
		choosedb = 3,
	}		

	responsemsg = dao.query(user_name, requestmsg)

	if responsemsg == nil then
		filelog.sys_error("PlayerauthDAO.query_player_uidbyusername failed because cannot access authdbsvrd")
		return false, nil		
	end

	if not responsemsg.issuccess then
		filelog.sys_error("PlayerauthDAO.query_player_uidbyusername failed because authdbsvrd exception")		
		return false, nil
	end

	if responsemsg.data == nil then
		return true, nil
	end

	if not responsemsg.isredisormysql then
		userinfo = json.decode(responsemsg.data)
	else
		userinfo = responsemsg.data[1]
		user_name = userinfo.user_name
		if user_name ~= nil then			
			PlayerauthDAO.save_player_userinfo("update", user_name, userinfo)
		end
	end

	return true, userinfo
end

--choosedb 1表示redis 2表示mysql 3表示redis和mysql
function PlayerauthDAO.save_player_userinfo(cmd, user_name, userinfo, choosedb)
	if cmd == nil or user_name == nil then
		filelog.sys_error("PlayerauthDAO.save_player_userinfo cmd == nil or user_name == nil")
		return
	end
	local noticemsg = {
		user_name = user_name,
		rediscmd = "set",
		rediskey = "user:"..user_name,
		--都是可选的使用时一定是按顺序添加
		rediscmdopt1 = json.encode(userinfo),
		mysqltable = "user_auth",
		mysqldata = userinfo,
		mysqlcondition = {
			user_name = user_name,
		},
		choosedb = 3,
	}
	if choosedb ~= nil then
		noticemsg.choosedb = choosedb
	end

	local f = dao[cmd]
	f(user_name, noticemsg)
end

--注册查询玩家账号是否注册过
function PlayerauthDAO.query_playerfrom_alldbbyusername(user_name)
	local authdbsvrs = configdao.get_svrs("authdbsvrs")
	local responsemsg
	if user_name == nil then
		filelog.sys_error("PlayerauthDAO.query_playerfrom_alldbbyusername user_name == nil")		
		return false, nil
	end
	local userinfo = nil
	local requestmsg = {
		user_name = user_name,
		rediscmd = "get",
		rediskey = "user:"..user_name,
		--都是可选的使用时一定是按顺序添加

		mysqltable = "user_auth",
		mysqlcondition = {
			user_name = user_name,
		},
		choosedb = 3,
	}		

	for svr_id, _ in pairs(authdbsvrs) do
		responsemsg = dao.querybysvrid(svr_id, requestmsg)
		if responsemsg == nil then
			filelog.sys_error("PlayerauthDAO.query_playerfrom_alldbbyusername failed because cannot access authdbsvrd")
			return false, nil		
		end

		if not responsemsg.issuccess then
			filelog.sys_error("PlayerauthDAO.query_playerfrom_alldbbyusername failed because authdbsvrd exception")		
			return false, nil
		end

		if responsemsg.data ~= nil then
			break
		end		
	end

	if responsemsg.data == nil then
		return true, nil
	end

	if not responsemsg.isredisormysql then
		userinfo = json.decode(responsemsg.data)
	else
		userinfo = responsemsg.data[1]
		user_name = userinfo.user_name
		if user_name ~= nil then			
			PlayerauthDAO.save_player_userinfo("update", user_name, userinfo)
		end
	end

	return true, userinfo
end

--注册查询玩家账号是否注册过
function PlayerauthDAO.query_playerfrom_alldbbyregmobileid(regmobileid)
	local authdbsvrs = configdao.get_svrs("authdbsvrs")
	local responsemsg
	if regmobileid == nil then
		filelog.sys_error("PlayerauthDAO.query_playerfrom_alldbbyregmobileid regmobileid == nil")		
		return false, nil
	end
	local userinfo = nil
	local requestmsg = {
		user_name = user_name,
		mysqltable = "user_auth",
		mysqlcondition = {
			regmobileid = regmobileid,
		},
		choosedb = 2,
	}		

	for svr_id, _ in pairs(authdbsvrs) do
		responsemsg = dao.querybysvrid(svr_id, requestmsg)
		if responsemsg == nil then
			filelog.sys_error("PlayerauthDAO.query_playerfrom_alldbbyregmobileid failed because cannot access authdbsvrd")
			return false, nil		
		end

		if not responsemsg.issuccess then
			filelog.sys_error("PlayerauthDAO.query_playerfrom_alldbbyregmobileid failed because authdbsvrd exception")		
			return false, nil
		end

		if responsemsg.data ~= nil then
			break
		end		
	end

	if responsemsg.data == nil then
		return true, nil
	end

	userinfo = responsemsg.data[1]
	user_name = userinfo.user_name
	if user_name ~= nil then			
		PlayerauthDAO.save_player_userinfo("update", user_name, userinfo)
	end

	return true, userinfo
end

function PlayerauthDAO.get_maxplayer_uid(svr_id)
	local id = string.match(svr_id, "%a*_(%d+)")
	local maxid = id * 20000000
	local minid = (id-1) * 20000000
	local maxuid
	local requestmsg = {
		user_name = "authdbsvrd",
		mysqlcondition = "select max(uid) as maxuid from user_auth where uid > "..minid.." and uid <= "..maxid,
		choosedb = 2,
	}

	local authdbsvrs = configdao.get_svrs("authdbsvrs")
	local svrsinfo = {}
	for svr_id, _ in pairs(authdbsvrs) do
		responsemsg = dao.querybysvrid(svr_id, requestmsg)
		if responsemsg == nil then
			filelog.sys_error("PlayerauthDAO.query_playerfrom_alldbbyusername failed because cannot access authdbsvrd")
			return false, nil		
		end

		if not responsemsg.issuccess then
			filelog.sys_error("PlayerauthDAO.query_playerfrom_alldbbyusername failed because authdbsvrd exception")		
			return false, nil
		end

		if responsemsg.data == nil 
			or responsemsg.data[1] == nil 
			or responsemsg.data[1].maxuid == nil then
			if minid == 0 then
				svrsinfo[svr_id] = 1000
			else	
				svrsinfo[svr_id] = minid
			end
		else
			svrsinfo[svr_id] = responsemsg.data[1].maxuid
		end	
	end		


	for _, uid in pairs(svrsinfo) do
		if maxuid == nil then
		   maxuid = uid
		end
		if maxuid < uid then
			maxuid = uid
		end
	end
	return true, maxuid
end

return PlayerauthDAO
